package org.lanqiao.lg.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Company;
import org.lanqiao.bean.Label;
import org.lanqiao.lg.dao.LabelDao;
import org.lanqiao.lg.dao.impl.LabelDaoImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/LabelServlet")
public class LabelServlet extends BaseServlet {
	Gson gson = new Gson();
	LabelDao labeldao = new LabelDaoImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key + "/id=" + id + "/typeU=" + typeU);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "label":
				insertLabel();
				break;
			case "searchContent":
				searchContent();
				break;
			case "select":
				selectL();
				break;
			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}

//	根据id查询标签内容
	private void selectL() {
		Integer id = getInt("label");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		String cinfo = labeldao.searchContent(id);
		if (cinfo != null) {
			redata.setCode(2);
			redata.setMsg("searchContent 成功");
			redata.setData(cinfo);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(cinfo);
			return;
		}

	}

//	添加标签前先查询是否有该标签  i 是标签 id
	private void insertLabel() {
		String content = getString("lcontent");
		System.out.println(content);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}

		String sub = content.substring(1, content.length() - 1);
		System.out.println(sub);
		String[] split = sub.split(",");
		for (int i = 0; i < split.length; i++) {
			split[i] = split[i].substring(1, split[i].length() - 1);
			System.out.println(split[i]);
		}

		String labelIds = "";

		for (String string : split) {
			System.out.println(labelIds);
//			先查询
			Integer i = labeldao.searchLabelId(new Label(string));
			System.out.println(i);
			if (i != null) {
				labelIds += (i + ",");
			} else {
//				查询没有就添加
				i = labeldao.insertLabel(new Label(string));
				if (i != null) {
					labelIds += (i + ",");
				}
			}
		}

		if (labelIds != "") {
			redata.setCode(2);
			redata.setMsg("insertContent 成功");
			redata.setData(labelIds);
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData("");
			return;
		}
	}

//	先获取公司表的标签集合labelId,截取出id,再查询content
	private void searchContent() {
		String labelIds = getString("labelId");
		System.out.println(labelIds);
		String[] ids = labelIds.split(",");
		String contents = "";
		for (int i = 0; i < ids.length; i++) {
//			System.out.println(ids[i]);
			String content = labeldao.searchContent(Integer.parseInt(ids[i]));
//			System.out.println(content);
			contents += (content + ",");
			System.out.println(contents);
		}

		if (contents != "") {
			redata.setCode(2);
			redata.setMsg("searchContent 成功");
			redata.setData(contents);
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData("");
			return;
		}
	}

}
